package com.netless.whitesdk;


import android.content.Context;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;

import com.herewhite.sdk.AbstractRoomCallbacks;
import com.herewhite.sdk.CommonCallbacks;
import com.herewhite.sdk.Room;
import com.herewhite.sdk.RoomParams;
import com.herewhite.sdk.WhiteSdk;
import com.herewhite.sdk.WhiteSdkConfiguration;
import com.herewhite.sdk.WhiteboardView;
import com.herewhite.sdk.domain.Promise;
import com.herewhite.sdk.domain.RoomPhase;
import com.herewhite.sdk.domain.RoomState;
import com.herewhite.sdk.domain.SDKError;

import java.util.Map;

import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.platform.PlatformView;

public class WhiteSdkView implements PlatformView, MethodChannel.MethodCallHandler {

  private WhiteboardView whiteboardView;
  private WhiteSdk whiteSdk;

  private Context context;
  private BinaryMessenger messenger;
  private int id;
  private Map<String, Object> params;
  private View containerView;
  private String appIdentifier;
  private Room room;
  private MethodChannel methodChannel;

  public WhiteSdkView(Context context, BinaryMessenger messenger, int id, Map<String, Object> params, View containerView) {
    this.context = context;
    this.messenger = messenger;
    this.id = id;
    this.params = params;
    this.containerView = containerView;
    this.whiteboardView = new WhiteboardView(context);
    this.methodChannel = new MethodChannel(messenger, "plugins.netless.link/whiteboard_" + id);
    this.methodChannel.setMethodCallHandler(this);
  }

  @Override
  public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
    if (call.method.equals("joinRoom")) {
      String uuid = call.argument("uuid");
      String roomToken = call.argument("roomToken");
      joinRoom(uuid, roomToken, result);
    }
    else if(call.method.equals("createSdk")) {
      String appId = call.argument("appId");
      boolean log = call.argument("log");
      WhiteSdkConfiguration sdkConfiguration = new WhiteSdkConfiguration(appId, log);
      this.appIdentifier = appId;
      this.whiteSdk = new WhiteSdk(whiteboardView, this.context, sdkConfiguration,
              new CommonCallbacks() {
                @Override
                public String urlInterrupter(String sourceUrl) {
                  return sourceUrl;
                }

                @Override
                public void sdkSetupFail(SDKError error) {
                  Log.e("ROOM_ERROR", error.toString());
                }

                @Override
                public void throwError(Object args) {

                }

                @Override
                public void onPPTMediaPlay() {

                }

                @Override
                public void onPPTMediaPause() {

                }
              });
    }
    else {
      result.notImplemented();
    }
  }

  @Override
  public View getView() {
    return whiteboardView;
  }

  @Override
  public void onFlutterViewAttached(@NonNull View view) {
    
  }

  @Override
  public void onFlutterViewDetached() {

  }

  @Override
  public void dispose() {
    whiteboardView.removeAllViews();
    whiteboardView.destroy();
  }

  private void joinRoom(String uuid, String roomToken, final MethodChannel.Result result) {
      Promise<Room> promise =  new Promise<Room>() {
        @Override
        public void then(Room wRoom) {
          room = wRoom;
          /// join in room success
          result.success(wRoom.getObserverId());
        }

        @Override
        public void catchEx(SDKError t) {
          /// join in room error
          result.error("joinRoomError", t.getMessage(), t.getJsStack());
        }
      };
      whiteSdk.joinRoom(
              new RoomParams(uuid, roomToken),
              new AbstractRoomCallbacks() {
                @Override
                public void onPhaseChanged(RoomPhase phase) {

                }

                @Override
                public void onRoomStateChanged(RoomState modifyState) {

                }
              },
              promise
      );
    }
}